package cn.jdy.plugins.service.xysgajM;

import java.util.List;
import java.util.Map;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.xysgajM.Userpri;

/**
 * @author wuzhiliang
 * @title : 文章操作实现
 */
@Service
public class UserpriService extends BaseDao<Userpri> {

	@Autowired
	private ConfService confService;

	/**
	 * 按条件查询
	 * @param NewsNewsXysgajM
	 * @return
	 */
	public List<Userpri> query(Userpri userpri, String... date) {
		if(userpri == null) return super.findAll();
		//return super.find(createFilter(NewsNewsXysgajM, NewsNewsXysgajM.getiDisplayLength() > 0, date));
		if(userpri.getiDisplayLength() > 0){
			return super.find(createFilter(userpri, date),userpri.getiDisplayStart(),userpri.getiDisplayLength());
		}
		return super.find(createFilter(userpri, date));
	}

	
	public Long count(Userpri userpri, String... date) {
		return super.count(createFilter(userpri, date));
	}
	/**
	 * 拼装条件
	 * @param NewsNewsXysgajM
	 * @param page
	 * @return
	 */
	private DetachedCriteria createFilter(Userpri userpri, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  if(!StringUtil.isNullOrEmpty(userpri.getKeywords())) {
      if(!StringUtil.isNullOrEmpty(userpri.getKeyType())) {
        dc.add(Restrictions.like(userpri.getKeyType(), userpri.getKeywords().trim(),MatchMode.ANYWHERE));
      }else
        //TODO 添加模糊搜索
        ;
    }
	  if(date.length>0 && !StringUtil.isNullOrEmpty(date[0]) && !StringUtil.isNullOrEmpty(date[1])){
		  dc.add(Restrictions.between("CreateTime", date[0],date[1]));
	  }
	  if(!StringUtil.isNullOrEmpty(userpri.getUsername())){
	    dc.add(Restrictions.eq("username", userpri.getUsername()));
	  }
	  if(!StringUtil.isNullOrEmpty(userpri.getUserid())){
		    dc.add(Restrictions.eq("userid", userpri.getUserid()));
		  }
	  dc.addOrder(Order.desc("id"));
		return dc;
	}

//给其他service使用   代虎
	public DetachedCriteria createFilteruser(String userid) {
		  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		  
			    dc.add(Restrictions.eq("userid", userid));
			  
			return dc;
		}
	
	/**
	 * @since 040116
	 * 模糊查询新闻
	 * @return
	 * 加入columnId
	 */
	public List<Userpri> queryByLike(Userpri userpri,String keywords) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  dc.add(Restrictions.eq("status", userpri.getStatus()));//前台查询需展示有效状态文章
	  dc.add(Restrictions.or(
	      Restrictions.like("title", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("keyword", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("summary", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("context", keywords, MatchMode.ANYWHERE)
	  ));
	  dc.addOrder(Order.desc("falseCreateTime"));
	  return super.find(dc,userpri.getiDisplayStart(),userpri.getiDisplayLength());
	}
	
	/**
	 * 模糊查询新闻
	 * @return
	 */
	public int queryByLikeCount(Userpri userpri,String keywords) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  dc.add(Restrictions.eq("status", userpri.getStatus()));//前台查询需展示有效状态文章
	  dc.add(Restrictions.or(
	      Restrictions.like("title", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("keyword", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("summary", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("context", keywords, MatchMode.ANYWHERE)
	  ));
	  dc.addOrder(Order.desc("falseCreateTime"));
	  return super.find(dc).size();
	}

	/**
	 * 按照类型查询 文章
	 * @return
	 */
	public List<Userpri> findbytype(Long columnid,int firstResult, int maxResults, boolean isImg) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
//	  前台查询需展示有效/置顶状态的文章
	  dc.add(Restrictions.or(Restrictions.eq("status", "1"),Restrictions.eq("status", "5"),Restrictions.eq("status", "6"),Restrictions.eq("status", "7")));
//	  dc.add(Restrictions.eq("status", "1"));//前台查询需展示有效状态文章
	  if(null != columnid){
	    dc.add(Restrictions.eq("columnId", columnid));
	  }
	  if(isImg){// 是否有封面图片
	    dc.add(Restrictions.ne("imgUrl", ""));
	  }
	  dc.addOrder(Order.desc("falseCreateTime"));
	  return super.find(dc, firstResult, maxResults);
	}
	
	/**
	 * 按照大分类 查询 文章
	 * @return
	 */
	public List<Userpri> findbyPid(Long pid,int firstResult, int maxResults, boolean isImg) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  dc.createAlias("cv", "cv");
//	  前台查询需展示有效/置顶状态的文章
//	  dc.add(Restrictions.or(Restrictions.eq("status", "1"),Restrictions.eq("status", "3")));
	  dc.add(Restrictions.eq("status", "1"));//前台查询需展示有效状态文章
	  if(isImg){// 是否有封面图片
	    dc.add(Restrictions.ne("imgUrl", ""));
    }
	  if(null != pid){
	    dc.add(Restrictions.eq("cv.pid", pid));
	  }
	  dc.addOrder(Order.desc("falseCreateTime"));
	  return super.find(dc,firstResult,maxResults);
	}
	
	/**
	 * 站点地图
	 * @return
	 */
	public List<Map<String, String>> querySiteMap(int start,int limit) {
	  String sql = "select id,title,createTime from "+entityClass.getSimpleName()+" where status =1 limit ?,?";
    SQLQuery sqlQuery = super.em().createSQLQuery(sql);
    sqlQuery.setParameter(0, start);
    sqlQuery.setParameter(1, limit);
    // 将查询寻结果转换为Map格式，解决多表查询或零散字段查询 映射取值问题
    sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
    return sqlQuery.list();
	}
 
	/**
	 * 查询置顶新闻
	 * @return
	 */
	public List<Userpri> queryStickieNewsNewsXysgajM() {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		dc.add(Restrictions.eq("status", "3"));//查询置顶状态的文章
    dc.addOrder(Order.desc("readTimes"));
    return super.find(dc,0,5);
	}
	
	/**
	 * 按照类型查询 文章
	 * @return
	 */
	public List<Userpri> findbytypeNstatus(Long columnid,int firstResult, int maxResults, boolean isImg, int isStickied) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
//	  前台查询需展示有效/置顶状态的文章
//	  dc.add(Restrictions.or(Restrictions.eq("status", "1"),Restrictions.eq("status", "3")));
	  dc.add(Restrictions.eq("status", "3"));//仅查询置顶状态-实际上仅一条
	  if(null != columnid){
	    dc.add(Restrictions.eq("columnId", columnid));
	  }
	  if(isImg){// 是否有封面图片
	    dc.add(Restrictions.ne("imgUrl", ""));
	  }
	  dc.addOrder(Order.desc("falseCreateTime"));
	  return super.find(dc, firstResult, maxResults);
	}
	
	public List<Userpri> querypage(Userpri userpri) {
		return super.find(createFilter(userpri),
				userpri.getPageNo(), 4);
	}
	
	public List<Userpri> querypagelaws(Userpri userpri) {
		return super.find(createFilter(userpri),
				userpri.getPageNo(), 10);
	}
}
